﻿using System;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Data.SqlServerCe;
using System.Collections.Generic;
using Mimoza.Common;

namespace Mimoza.DBLayer.SQLSC
{
    public class SQLSC_DBLayer: Mimoza.Common.IDBLayer
    {
        private SqlCeConnection connection = null;
        private SQLCS_DBContainer DBContainer = null;
        private SQLCS_DBHost DBHost = null;
        private SQLCS_DBPolicy DBPolicy = null;
        private SQLCS_DBUpdate DBUpdate = null;
        private string m_databaseName = null;

        public string DataBaseName
        {
            set { m_databaseName = value; }
        }   

        #region IDBLayer Members

        public void Connect()
        {
            if (m_databaseName == null) m_databaseName = SQLCS_Helper.GetDBName(null); 

            if (!SQLCS_Helper.IsDBExists(m_databaseName))
            {
                string dirName = Path.GetDirectoryName(m_databaseName);
                if (!string.IsNullOrEmpty(dirName)) Directory.CreateDirectory(dirName);
                SQLCS_Helper.CreateDB(m_databaseName,true);
            }

            connection = new SqlCeConnection(SQLCS_Helper.GetConnectionString(m_databaseName));
            connection.Open();

            DBContainer = new SQLCS_DBContainer(connection);
            DBHost = new SQLCS_DBHost(connection);
            DBPolicy = new SQLCS_DBPolicy(connection);
            DBUpdate = new SQLCS_DBUpdate(connection);
        }
        public void ConnectToHostDB()
        {
            if (!SQLCS_Helper.IsDBExists(m_databaseName))
            {
                string dirName = Path.GetDirectoryName(m_databaseName);
                if (!string.IsNullOrEmpty(dirName)) Directory.CreateDirectory(dirName);
                SQLCS_Helper.CreateDB(m_databaseName, false);
            }

            connection = new SqlCeConnection(SQLCS_Helper.GetConnectionString(m_databaseName));
            connection.Open();

            DBContainer = new SQLCS_DBContainer(connection);
            DBHost = new SQLCS_DBHost(connection);
            DBPolicy = new SQLCS_DBPolicy(connection);
            DBUpdate = new SQLCS_DBUpdate(connection);
        }
        public void Disconnect()
        {
            if (connection != null)
            {
                DBContainer = null;
                DBHost = null;
                DBPolicy = null;
                DBUpdate = null;

                connection.Close();
                connection = null;
            }
        }

        public IDBContainerRepository DBContainerRepository
        {
            get
            {
                //if (DBContainer == null) throw new Exception("Connection not opened");
                return DBContainer;
            }
        }

        public IDBHostRepository DBHostRepository
        {
            get
            {
                if (DBHost == null) throw new Exception("Connection not opened");
                return DBHost;
            }
        }

        public IDBPolicyRepository DBPolicyRepository
        {
            get
            {
                if (DBPolicy == null) throw new Exception("Connection not opened");
                return DBPolicy;
            }
        }

        public IDBUpdateRepository DBUpdateRepository
        {
            get
            {
                if (DBUpdate == null) throw new Exception("Connection not opened");
                return DBUpdate;
            }
        }

        #endregion
    }
}

